package cn.cyl.pet.service.impl;

import cn.cyl.basic.service.BaiduAiAuditService;
import cn.cyl.basic.service.impl.BaseServiceImpl;
import cn.cyl.basic.util.AjaxResult;
import cn.cyl.pet.domain.Pet;
import cn.cyl.pet.domain.PetDetail;
import cn.cyl.pet.domain.PetOnlineAuditLog;
import cn.cyl.pet.mapper.PetDetailMapper;
import cn.cyl.pet.mapper.PetMapper;
import cn.cyl.pet.mapper.PetOnlineAuditLogMapper;
import cn.cyl.pet.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 宠物增删改的时候同步宠物详情
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private BaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Transactional
    @Override
    public void save(Pet pet) {
        //添加一个新的宠物
        super.save(pet);
        //获取该宠物的详情
        PetDetail petDetail=pet.getPetDetail();
        if(petDetail!=null){//如果获取到的有值
            //就将pet的ID 设置到宠物类型的petId
            petDetail.setPetId(pet.getId());
            //添加进数据库
            petDetailMapper.save(petDetail);
        }

    }


    @Transactional
    @Override
    public void del(Long id) {
        //根据宠物的ID来删除宠物详情
        petDetailMapper.delPetDetailByPetId(id);
        super.del(id);
    }



    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }


    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    /**
     * 下架
     * @param ids
     */
    @Transactional
    @Override
    public void offsale(List<Long> ids) {

        //修改状态state=0   设置下架时间
        petMapper.updateOffsale(ids,new Date());

    }


    /**
     * 上架
     * @param ids
     */
    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.loadOne(id);
            //ai审核
            Boolean boolean1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boolean2 = baiduAiAuditService.imageAudit(pet.getResources());
            if(boolean1 && boolean2){//审核通过
                petMapper.updateOnsaleState(id,new Date());
                //添加审核日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setNote("审核成功");
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
            }else {
                //添加审核日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setNote("审核失败");
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg(pet.getName()+"审核失败");
                return ajaxResult;
            }
        }
        return new AjaxResult();


    }
}
